package com.cy.pj.common.config;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

//@Configuration
@EnableAsync //启动异步操作(底层会创建一个池)
@Slf4j
public class SpringAsyncConfig implements AsyncConfigurer{
	//@Value注解自动从配置文件读取数据注入给属性
	@Value("${executor.corePoolSize}")
	private int corePoolSize=10;
	@Value("${executor.maximumPoolSize}")
	private int maximumPoolSize=20;
	@Value("${executor.keepAliveTime}")
	private int keepAliveTime=30;
	@Value("${executor.queueCapacity}")
	private int queueCapacity=20;
	private ThreadFactory threadFactory = new ThreadFactory() {
		private AtomicLong number=new AtomicLong(100);
		@Override
		public Thread newThread(Runnable r) {
			return new Thread(r,"async-thread-"+number.getAndIncrement());
		}
	};
	//JDK 自带的池(Tomcat默认也是使用这个池)
	@Bean("asyncThreadPool")
	public Executor newThreadPoolExecutor() {
		ThreadPoolExecutor pool=
		new ThreadPoolExecutor(corePoolSize,
				maximumPoolSize,
				keepAliveTime,
				TimeUnit.SECONDS,
				new LinkedBlockingQueue<>(queueCapacity),
				threadFactory);
		return pool;
	}
	@Override
	public Executor getAsyncExecutor() {
		System.out.println("corePoolSize="+corePoolSize);
		System.out.println("==getAsyncExecutor()==");
        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
        pool.setCorePoolSize(corePoolSize);
        pool.setMaxPoolSize(maximumPoolSize);
        pool.setKeepAliveSeconds(keepAliveTime);
        pool.setQueueCapacity(queueCapacity);
        pool.setQueueCapacity(queueCapacity);
        //设置拒绝执行的处理
        pool.setRejectedExecutionHandler(new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                log.error("队列满了,拒绝执行");
            }
        });
        pool.initialize();
		return pool;
	}

}






